package com.offcn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.offcn.mapper.MemberMapper;
import com.offcn.mapper.OrderMapper;
import com.offcn.pojo.Member;
import com.offcn.pojo.Order;
import com.offcn.pojo.Setmeal;
import com.offcn.service.MemberService;
import com.offcn.utils.DateUtils;
import com.offcn.utils.MessageConstant;
import com.offcn.utils.Result;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: offcnpe_parent
 * @description:
 * @author: xin yi
 * @create: 2021-07-22 09:41
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Result getMemberReport() {
        /*月份集合*/
        List<String> months = new ArrayList<>();
        /*每个月的会员数量集合*/
        List<Integer> countList = new ArrayList<>();

        /*获得日历对象*/
        Calendar calendar = Calendar.getInstance();
        /*计算过去十二个月*/
        calendar.add(Calendar.MONTH,-12);

        for (int i = 0;i<12;i++){
            /*获取当前时间往后推一个月时间*/
            calendar.add(Calendar.MONTH,1);
            Date date = calendar.getTime();

            String month = new SimpleDateFormat("yyyy.MM").format(date);
            months.add(month);
//            String startMonth = months + ".1";
//            String endtMonth = months + ".31";
            String currentMonth = month +".31";
            QueryWrapper<Member> wrapper = new QueryWrapper<>();
//            wrapper.between("regTime",startMonth,endtMonth);
            wrapper.lt("regTime",date);
            int count = memberMapper.selectCount(wrapper);
            countList.add(count);

        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("months",months);
        map.put("memberCount",countList);
        return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS,map);
    }

    @Override
    public Result r_getBusinessReportData() {
        try {
            /*获取当前日期*/
            String today = DateUtils.parseDate2String(DateUtils.getToday());
            /*获得本周一的日期*/
            String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
            /*获得本月一第一天的日期*/
            String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
            /*获取本周日的日期*/
            String lastDayOfWeek= DateUtils.parseDate2String(DateUtils.getLastDayOfWeek(new Date()));

            /*本日新增会员数*/
            QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
            memberQueryWrapper.eq("regTime",today);
            Integer todayNewMember = memberMapper.selectCount(memberQueryWrapper);

            /*总会员数*/
            Integer totalMember = memberMapper.selectCount(null);

            /*本周新增会员数*/
            QueryWrapper<Member> memberQueryWrapper1 = new QueryWrapper<>();
            memberQueryWrapper1.between("regTime",thisWeekMonday,lastDayOfWeek);
            Integer thisWeekNewMember = memberMapper.selectCount(memberQueryWrapper1);

            /*本月新增会员数*/
            QueryWrapper<Member> memberQueryWrapper2 = new QueryWrapper<>();
            memberQueryWrapper2.gt("regTime",firstDay4ThisMonth);
            Integer thisMonthNewMember = memberMapper.selectCount(memberQueryWrapper2);

            /*今日预约人数*/
            QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
            orderQueryWrapper.eq("orderDate",today);
            Integer todayOrderNumber = orderMapper.selectCount(orderQueryWrapper);

            /*本周预约人数*/
            QueryWrapper<Order> orderQueryWrapper4 = new QueryWrapper<>();
            orderQueryWrapper4.between("orderDate",thisWeekMonday,lastDayOfWeek);
            Integer thisWeekOrderNumber = orderMapper.selectCount(orderQueryWrapper4);

            /*本月预约人数*/
            QueryWrapper<Order> orderQueryWrapper5 = new QueryWrapper<>();
            orderQueryWrapper5.gt("orderDate",firstDay4ThisMonth);
            Integer thisMonthOrderNumber = orderMapper.selectCount(orderQueryWrapper5);

            /*今日到诊人数*/
            QueryWrapper<Order> orderQueryWrapper6 = new QueryWrapper<>();
            orderQueryWrapper6.eq("orderDate",today);
            orderQueryWrapper6.notIn("orderType","未到诊")
                    .or()
                    .notIn("orderType","null");
            Integer todayVisitsNumber = orderMapper.selectCount(orderQueryWrapper6);

            /*本周到诊人数*/
            QueryWrapper<Order> orderQueryWrapper1 = new QueryWrapper<>();
            orderQueryWrapper1.between("orderDate",thisWeekMonday,lastDayOfWeek);
            orderQueryWrapper1.eq("orderDate",today);
            orderQueryWrapper1.notIn("orderType","未到诊")
                    .or()
                    .notIn("orderType","null");
            Integer thisWeekVisitsNumber = orderMapper.selectCount(orderQueryWrapper1);

            /*本月到诊人数*/
            QueryWrapper<Order> orderQueryWrapper2 = new QueryWrapper<>();
            orderQueryWrapper2.gt("orderDate",firstDay4ThisMonth);
            orderQueryWrapper2.eq("orderDate",today);
            orderQueryWrapper2.notIn("orderType","未到诊")
                    .or()
                    .notIn("orderType","null");
            Integer thisMonthVisitsNumber = orderMapper.selectCount(orderQueryWrapper2);

            /*热门套餐查询*/
//            QueryWrapper<Order> orderQueryWrapper3 = new QueryWrapper<>();
//            QueryWrapper<Setmeal> setmealQueryWrapper = new QueryWrapper<>();
//            QueryWrapper<Setmeal> setmeal_id = setmealQueryWrapper.eq("setmeal_id", new Setmeal().getId());
//            orderQueryWrapper3.groupBy(String.valueOf(setmeal_id));
//            QueryWrapper<Setmeal> setmeal_id1 = setmealQueryWrapper.eq("setmeal_id", new Setmeal().getId());
//            orderQueryWrapper3.orderByDesc(String.valueOf(setmeal_id1));
//            List<Map<String, Object>> hotSetmeal01 = orderMapper.selectMaps(orderQueryWrapper3);
//            List<Map> hotSetmeal02 = new ArrayList<>();
//            for (Map<String, Object> map : hotSetmeal01) {
//                hotSetmeal02.add(map);
//            }

//
//            List<Map<String, Object>> hotSetmeal01 = orderMapper.selectMaps(orderQueryWrapper3);
//            List<Map> hotSetmeal02 = new ArrayList<>();
//            for (Map<String, Object> map : hotSetmeal01) {
//                hotSetmeal02.add(map);
//            }


            /*把数据放进去*/
            HashMap<String, Object> result = new HashMap<>();
            result.put("reportDate",today);
            result.put("todayNewMember",todayNewMember);
            result.put("totalMember",totalMember);
            result.put("thisWeekNewMember",thisWeekNewMember);
            result.put("thisMonthNewMember",thisMonthNewMember);
            result.put("todayOrderNumber",todayOrderNumber);
            result.put("thisWeekOrderNumber",thisWeekOrderNumber);
            result.put("thisMonthOrderNumber",thisMonthOrderNumber);
            result.put("todayVisitsNumber",todayVisitsNumber);
            result.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
            result.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
//            result.put("hotSetmeal",hotSetmeal);
            result.put("hotSetmeal",null);

            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }
}
